package com.sskj.ffex.v.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.ffex.MyAppLication;
import com.sskj.ffex.R;
import com.sskj.ffex.common.App;
import com.sskj.ffex.common.Constants;
import com.sskj.ffex.common.adapter.BaseAdapter;
import com.sskj.ffex.common.adapter.ViewHolder;
import com.sskj.ffex.common.widget.SelectView;
import com.sskj.ffex.m.bean.HttpData;
import com.sskj.ffex.m.bean.PayWayBean;
import com.sskj.ffex.m.bean.UserInfo;
import com.sskj.ffex.p.activity.SecurityCenterPresenter;
import com.sskj.ffex.utils.BtnDoubleClickUtil;
import com.sskj.ffex.utils.ClickUtil;
import com.sskj.ffex.utils.PictureUtil;
import com.sskj.ffex.utils.SPUtils;
import com.sskj.ffex.utils.ToastUtil;
import com.sskj.ffex.v.base.BaseActivity;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.sskj.ffex.common.Constants.PAY_PWD_SETTING_CODE;
import static com.sskj.ffex.common.Constants.VERIFY_IDENT_CODE;

/**
 * 安全中心
 * create by Hey at 2018/4/20 15:04
 */
public class SecurityCenterActivity extends BaseActivity<SecurityCenterPresenter> implements View.OnClickListener {

    @BindView(R.id.security_mobile_layout)
    FrameLayout mobileLayout;
    @BindView(R.id.security_email_layout)
    FrameLayout emailLayout;
    @BindView(R.id.security_mobile_checked)
    TextView mSecurityMobileChecked;
    @BindView(R.id.security_bottom_recyclerview)
    RecyclerView mSecurityRecyclerView;
    @BindView(R.id.security_email_checked)
    TextView mSecurityEmailChecked;
    @BindView(R.id.security_login_paw_checked)
    TextView mSecurityLoginPawChecked;
    @BindView(R.id.security_pay_psw_checked)
    TextView mSecurityPayPswChecked;
    @BindView(R.id.security_verify_checked)
    TextView mSecurityVerifyChecked;
    @BindView(R.id.google_verify_checked)
    TextView mGoogleVerifyChecked;
    @BindView(R.id.toolbar)
    public Toolbar mToolbar;
    @BindView(R.id.collapsing_toolbar)
    public CollapsingToolbarLayout mCollapsingToolbar;
    @BindView(R.id.security_apply_business_checked)
    TextView mSecurityApplyBusinessChecked;
    private UserInfo mUserInfo;
    private BaseAdapter<PayWayBean> mAdapter;
    /**
     * 用户选择的支付方式
     */
    private String mSelectName;
    private EditText mDialogInputBankName;
    private EditText mDialogInputBankCardNum;
    private View mDialogThirdpartyLayout;
    private TextView mDialogThirdpartyName;
    private EditText mDialogInputThirdpartyAccount;
    private EditText dialogInputPaypalName;
    private ImageView mDialogThirdpartyPic;
    private SelectView mDialogPayWaySpinner;
    private TextView mDialogRealname;
    private View mDialogCancel;
    private View mDialogSubmit;
    private View mDialogBanknumLayout;
    private View dialogPaypalLayout;
    private AlertDialog mAlertDialog;
    private View mDialogClose;
    private EditText mDialogInputPayPwd;
    private View mFooterView;
    private View mFailView;
    private View mStartLading;
    private EditText mDialogInputBankBranch;
    private TextView mDialogThirdpartyPicHint;
    /**
     * 支付方式 微信，支付宝 收款二维码 图片URL
     */
    private String mImagePath;
    /**
     * 支付方式添加 或 编辑  add/edit
     */
    private String mType = "add";
    private View mLoadingProgress;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_security_center;
    }

    @Override
    public SecurityCenterPresenter getPresenter() {
        return new SecurityCenterPresenter();
    }

    @Override
    protected void onViewBinding() {
//        mTitle.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity201));
//        mToolBar.setLeftButtonOnClickLinster(v -> finish());
//        Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
//        mCollapsingToolbar.setExpandedTitleTypeface(font);//加粗
        mToolbar.setTitle(App.INSTANCE.getString(R.string.strSecurityCenterActivity201));
        mToolbar.setNavigationIcon(R.mipmap.icon_back);
        //        mToolbar.setContentInsetStartWithNavigation(0);
//        mToolbar.inflateMenu(R.menu.toolbar_null_menu);
        mToolbar.setNavigationOnClickListener(v -> finish());
//        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                return true;
//            }
//        });

        mSecurityRecyclerView.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mSecurityRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        mAdapter = new BaseAdapter<PayWayBean>(R.layout.layout_pay_way, null, mSecurityRecyclerView) {

            @Override
            public void bind(ViewHolder holder, PayWayBean item) {
                holder.setText(R.id.item_type_name, item.getTip());
                holder.setText(R.id.item_content, String.format("%1$s\t\t%2$s\t\t%3$s", item.getNumber(), item.getName(), item.getTip()));
                holder.setOnClickListener(R.id.item_edit, v -> {//修改支付方式
                    showDialog(item);
                });
                holder.setOnClickListener(R.id.item_switch, v -> {//切换支付方式开关
                    mPresenter.requsetCheckSwitch(item);
                });
                if (item.getStatus() == 0) {
                    holder.setChecked(R.id.item_switch, false);
                } else {
                    holder.setChecked(R.id.item_switch, true);
                }
                if ("wx".equals(item.getType())) {
                    holder.setImageResource(R.id.imageView, R.mipmap.icon_wx);
                } else if ("alipay".equals(item.getType())) {
                    holder.setImageResource(R.id.imageView, R.mipmap.icon_ali);
                } else if ("backcard".equals(item.getType())) {
                    holder.setImageResource(R.id.imageView, R.mipmap.icon_bank);
                } else if ("paypal".equals(item.getType())) {
                    holder.setImageResource(R.id.imageView, R.mipmap.pay_pal);
                }
            }
        };
        //新增支付方式view
        mFooterView = LayoutInflater.from(this).inflate(R.layout.item_text, null);
        mFooterView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkIdent()) {
                    if (mUserInfo != null) {
                        if (!TextUtils.isEmpty(mUserInfo.tpwd)) {
                            if ("1".equals(mUserInfo.tpwd)) {
                                showDialog(null);
                            }
                        } else {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(App.INSTANCE.getString(R.string.strSecurityCenterActivity208));
                            openActivity(PayPwdSettingActivity.class, PAY_PWD_SETTING_CODE);
                        }
                    }

                }
            }
        });
        //网络链接失败，重新加载view
        mFailView = LayoutInflater.from(this).inflate(R.layout.layout_reset_loading, null);
        mStartLading = mFailView.findViewById(R.id.start_loading);
        mLoadingProgress = mFailView.findViewById(R.id.loading_progress);
        mStartLading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mStartLading.setVisibility(View.INVISIBLE);
                mLoadingProgress.setVisibility(View.VISIBLE);
                mPresenter.requsetPayWayList();
            }
        });

        mPresenter.requsetPayWayList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.request();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constants.BIND_MOBILE_CODE:
                    if (mUserInfo != null) {
                        String mobile = data.getStringExtra("resultData");
                        mUserInfo.mobile = mobile;
                        mSecurityMobileChecked.setText(mobile);
//                        RxBus.getDefault().post(mUserInfo);
                        ClickUtil.click(mobileLayout, () -> {
                        });
                    }
                    break;

                case Constants.BIND_EMAIL_CODE:
                    if (mUserInfo != null) {
                        String email = data.getStringExtra("resultData");
                        mUserInfo.mail = email;
                        mSecurityEmailChecked.setText(email);
//                        RxBus.getDefault().post(mUserInfo);
                        ClickUtil.click(emailLayout, () -> {
                        });
                    }
                    break;
                case Constants.PAY_PWD_SETTING_CODE:
                    if (mUserInfo != null) {
                        mUserInfo.tpwd = "1";
                        mSecurityPayPswChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity211));
//                        RxBus.getDefault().post(mUserInfo);
                    }
                    break;
                case VERIFY_IDENT_CODE:
                    if (mUserInfo != null) {
                        int status = data.getIntExtra("resultData", 1);
                        if (status == 2) {
                            mUserInfo.status = 2;
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS, 2);
                            mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity213));
                        }
//                        RxBus.getDefault().post(mUserInfo);
                    }
                    break;
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    mImagePath = selectList.get(0).getPath();
                    mDialogThirdpartyPic.setImageBitmap(PictureUtil.getSmallBitmap(selectList.get(0).getPath()));

                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    //                    adapter.setList(selectList);
                    //                    adapter.notifyDataSetChanged();
                    break;
            }

        }
    }

    @OnClick({R.id.google_verify_layout, R.id.security_apply_business_layout, R.id.security_login_paw_layout, R.id.security_pay_psw_layout, R.id.security_verify_layout})
    public void onViewClicked(View view) {
        if (mUserInfo != null && !BtnDoubleClickUtil.isFastDoubleClick()) {
            switch (view.getId()) {
                case R.id.security_login_paw_layout:  // 登录密码
                    Intent intent = new Intent(mActivity, PwdResetActivity.class);
                    intent.putExtra(Constants.IS_RESET, false);
                    startActivity(intent);
                    break;
                case R.id.security_pay_psw_layout:  // 支付密码
                    if (checkIdent()) {
                        openActivity(PayPwdSettingActivity.class, PAY_PWD_SETTING_CODE);
                    }
                    break;
                case R.id.security_verify_layout:  // 实名认证
                    if (checkIdent()) {  // 初级实名认证成功
                        if (checkIdentHign(this)) {  // 高级实名认证成功
                            startActivity(new Intent(this, CommonHighActivity.class));
                        }
                    }
                    break;
                case R.id.google_verify_layout:  // 谷歌认证
                    if (mUserInfo != null && mUserInfo.getCommand() == 0) {
                        startActivity(new Intent(this, VerifyGoogleActivity.class));
                    } else {
                        ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(App.INSTANCE.getString(R.string.strSecurityCenterActivity214));
                    }
                    break;
                case R.id.security_apply_business_layout:  // 商家申请
                    if (checkIdent()) {
                        if (checkPWd()) {
                            if (mUserInfo.is_shop) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyDialogStyleBottom);
                                View inflater = LayoutInflater.from(mActivity).inflate(R.layout.dialog_relieve_business, null);
                                AlertDialog alertDialog = builder.setView(inflater).show();
                                inflater.findViewById(R.id.dialog_relieve_cancel).setOnClickListener(v -> alertDialog.dismiss());
                                inflater.findViewById(R.id.dialog_relieve_confirm).setOnClickListener(v -> mPresenter.requestRelieveBusiness(alertDialog));
                            } else {
                                SellerApplyActivity.start(this);
                            }
                        }

                    }
                    break;
            }
        }
    }

    public boolean checkPWd() {
        if (TextUtils.isEmpty(mUserInfo.tpwd)) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.strLevelTransactionFragmentX279));
            startActivity(new Intent(mActivity, PayPwdSettingActivity.class));
            return false;
        } else {
            return true;
        }
    }

    public void updataStatus(UserInfo user) {
        mUserInfo = user;
        if (!TextUtils.isEmpty(user.mobile)) {
            mSecurityMobileChecked.setText(user.mobile);
            ClickUtil.click(mobileLayout, () -> {
            });
        } else {
            mSecurityMobileChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity215));
            ClickUtil.click(mobileLayout, () -> {
                openActivity(BindMobileActivity.class, Constants.BIND_MOBILE_CODE);
            });

        }
        if (!TextUtils.isEmpty(user.mail)/* && CheckUtil.isEmail(user.mail)*/) {
            mSecurityEmailChecked.setText(user.mail);
            ClickUtil.click(emailLayout, () -> {
            });
        } else {
            mSecurityEmailChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity215));
            ClickUtil.click(emailLayout, () -> {
                openActivity(BindEmailActivity.class, Constants.BIND_EMAIL_CODE);
            });
        }
        mSecurityLoginPawChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity211));

        if (user.tpwd != null && user.tpwd.equals("1")) {
            mSecurityPayPswChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity211));
        } else {
            mSecurityPayPswChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity219));
        }
        if (user.getCommand() == 1) {
            mGoogleVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity220));
        } else {
            mGoogleVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity221));
        }

        switch (user.status) {  // 实名认证状态
            case 1:
                mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity222));
                break;
            case 2:
                mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity223));
                break;
            case 3:
                mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity224));
                switch (user.getAuth_status()) {
                    case 2:
                        mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity225));
                        break;
                    case 3:
                        mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity226));
                        break;
                    case 4:
                        mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity227));
                        break;
                }
                break;
            case 4:
                mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity228));
                //                mSecurityVerifyChecked.setTextColor();
                break;
        }

        if (user.is_shop) {
            mSecurityApplyBusinessChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity229));
        } else {
            mSecurityApplyBusinessChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity221));
        }
    }

    /**
     * 添加支付方式
     *
     * @param item
     */
    public void showDialog(PayWayBean item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyDialogStyleBottom);
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_edit_pay_way, null);
        builder.setView(inflate);
        mDialogBanknumLayout = inflate.findViewById(R.id.dialog_banknum_layout);
        dialogPaypalLayout = inflate.findViewById(R.id.dialog_paypal_layout);
        mDialogInputBankName = inflate.findViewById(R.id.dialog_input_bank_name);
        mDialogInputBankCardNum = inflate.findViewById(R.id.dialog_input_bankcard_num);
        mDialogInputBankBranch = inflate.findViewById(R.id.dialog_input_bank_branch_name);
        mDialogThirdpartyLayout = inflate.findViewById(R.id.dialog_thirdparty_layout);
        mDialogThirdpartyName = inflate.findViewById(R.id.thirdparty_name);
        mDialogInputThirdpartyAccount = inflate.findViewById(R.id.dialog_input_thirdparty_account);
        dialogInputPaypalName = inflate.findViewById(R.id.dialog_input_paypal_name);
        mDialogThirdpartyPic = inflate.findViewById(R.id.dialog_thirdparty_pic);
        mDialogThirdpartyPicHint = inflate.findViewById(R.id.dialog_thirdparty_pic_hint);
        mDialogPayWaySpinner = inflate.findViewById(R.id.dialog_pay_way_spinner);
        if (item == null) {  // 添加支付方式
            mDialogPayWaySpinner.setEnabled(true);
        } else {  // 修改支付方式
            mDialogPayWaySpinner.setEnabled(false);
        }
        mDialogRealname = inflate.findViewById(R.id.dialog_realname);
        mDialogInputPayPwd = inflate.findViewById(R.id.dialog_input_pay_pwd);
        CheckBox checkBox = inflate.findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDialogInputPayPwd.setTransformationMethod(new HideReturnsTransformationMethod());
            } else {
                mDialogInputPayPwd.setTransformationMethod(new PasswordTransformationMethod());
            }
        });
        mDialogClose = inflate.findViewById(R.id.dialog_close);
        mDialogCancel = inflate.findViewById(R.id.dialog_cancel);
        mDialogSubmit = inflate.findViewById(R.id.dialog_submit);  // 完成设置
        String[] data = new String[]{App.INSTANCE.getString(R.string.strSecurityCenterActivity231), App.INSTANCE.getString(R.string.strSecurityCenterActivity232), App.INSTANCE.getString(R.string.strSecurityCenterActivity233), App.INSTANCE.getString(R.string.paypal)};
        mDialogPayWaySpinner.setText(data[0]);
        mDialogPayWaySpinner.bindAdapter(new BaseAdapter<String>(R.layout.item_spinner_exchange, Arrays.asList(data), null) {
            @Override
            public void bind(ViewHolder holder, String item) {
                TextView textView = holder.getView(R.id.text);
                textView.setText(item);
                textView.setOnClickListener(v -> {
                    mDialogPayWaySpinner.setText(item);
                    mDialogPayWaySpinner.dismiss();
                    mSelectName = item;
                    checkLayout();
                });
            }
        });


//        mDialogPayWaySpinner.setAdapter(stringArrayAdapter);
//        mDialogPayWaySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                mSelectName = stringArrayAdapter.getItem(position);
//                checkLayout();
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//                mDialogPayWaySpinner.setSelection(0, true);
//                mSelectName = stringArrayAdapter.getItem(0);
//                checkLayout();
//            }
//        });

        if (item != null) {
            if ("backcard".equals(item.getType())) {
                mDialogPayWaySpinner.setText(data[0]);
                mSelectName = data[0];
                checkLayout();

                mDialogInputBankName.setText(item.getBank());
                mDialogInputBankBranch.setText(item.getBranch());
                mDialogInputBankCardNum.setText(item.getNumber());
            } else if ("wx".equals(item.getType())) {
                mDialogPayWaySpinner.setText(data[1]);
                mSelectName = data[1];
                checkLayout();
                mDialogInputThirdpartyAccount.setText(item.getNumber());
            } else if ("alipay".equals(item.getType())) {
                mDialogPayWaySpinner.setText(data[2]);
                mSelectName = data[2];
                checkLayout();
                mDialogInputThirdpartyAccount.setText(item.getNumber());
            } else if ("paypal".equals(item.getType())) {
                mDialogPayWaySpinner.setText(data[3]);
                mSelectName = data[3];
                checkLayout();
                dialogInputPaypalName.setText(item.getNumber());
            }
            mDialogRealname.setText(item.getName());
            mType = "edit";
        } else {
            mType = "add";
            mDialogPayWaySpinner.setText(data[0]);
            mSelectName = data[0];
            checkLayout();
            mDialogRealname.setText(MyAppLication.getName());
        }
        mDialogSubmit.setOnClickListener(this);
        mDialogClose.setOnClickListener(this);
        mDialogCancel.setOnClickListener(this);
        mDialogThirdpartyPic.setOnClickListener(this);
        mAlertDialog = builder.create();
        mAlertDialog.show();
    }

    private void checkLayout() {
        if (App.INSTANCE.getString(R.string.strSecurityCenterActivity231).equals(mSelectName)) {
            mDialogBanknumLayout.setVisibility(View.VISIBLE);  // 银行卡相关信息
            mDialogThirdpartyLayout.setVisibility(View.GONE);  // 支付宝 微信相关信息
            dialogPaypalLayout.setVisibility(View.GONE);  // PAYpal账户
        } else if (App.INSTANCE.getString(R.string.strSecurityCenterActivity232).equals(mSelectName)) {
            mDialogBanknumLayout.setVisibility(View.GONE);
            mDialogThirdpartyLayout.setVisibility(View.VISIBLE);
            dialogPaypalLayout.setVisibility(View.GONE);
            mDialogThirdpartyName.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity241));
            mDialogInputThirdpartyAccount.setHint(String.format(getString(R.string.thirdparty_account), App.INSTANCE.getString(R.string.strSecurityCenterActivity232)));
            mDialogThirdpartyPicHint.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity243));
        } else if (App.INSTANCE.getString(R.string.strSecurityCenterActivity233).equals(mSelectName)) {
            dialogPaypalLayout.setVisibility(View.GONE);
            mDialogBanknumLayout.setVisibility(View.GONE);
            mDialogThirdpartyLayout.setVisibility(View.VISIBLE);
            mDialogThirdpartyName.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity245));
            mDialogInputThirdpartyAccount.setHint(String.format(getString(R.string.thirdparty_account), App.INSTANCE.getString(R.string.strSecurityCenterActivity233)));
            mDialogThirdpartyPicHint.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity247));
        } else if (App.INSTANCE.getString(R.string.paypal).equals(mSelectName)) {
            dialogPaypalLayout.setVisibility(View.VISIBLE);
            mDialogBanknumLayout.setVisibility(View.GONE);
            mDialogThirdpartyLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if (!BtnDoubleClickUtil.isFastDoubleClick()) {
            switch (v.getId()) {
                case R.id.dialog_submit:
                    String bankName = mDialogInputBankName.getText().toString().trim();  // 银行开户行
                    String bankBranch = mDialogInputBankBranch.getText().toString().trim();  // 开户银行支行
                    String bankNum = mDialogInputBankCardNum.getText().toString().trim();  // 银行卡号
                    String account = mDialogInputThirdpartyAccount.getText().toString().trim();  // 支付宝账号
                    String payPwd = mDialogInputPayPwd.getText().toString().trim();  // 资金密码
                    String paypalAccount = dialogInputPaypalName.getText().toString().trim();  // Paypal账户
                    String payName = "";
                    if (App.INSTANCE.getString(R.string.strSecurityCenterActivity231).equals(mSelectName)) {
                        if (TextUtils.isEmpty(bankName)) {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.empty_bank_name);
                            return;
                        }
                        if (TextUtils.isEmpty(bankBranch)) {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.empty_bank_branch);
                            return;
                        }
                        if (TextUtils.isEmpty(bankNum)) {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.empty_bank_card_num);
                            return;
                        }
                        if (bankNum.length() < 10 || bankNum.length() > 20) {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.error_bank_card_num);
                            return;
                        }
                        payName = "backcard";
                    } else if (App.INSTANCE.getString(R.string.strSecurityCenterActivity232).equals(mSelectName)) {
                        if (TextUtils.isEmpty(account)) {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.empty_wx_account);
                            return;
                        }
                        if (TextUtils.isEmpty(mImagePath)) {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.empty_pay_image_url);
                            return;
                        }
                        payName = "wx";
                    } else if (App.INSTANCE.getString(R.string.strSecurityCenterActivity233).equals(mSelectName)) {
                        if (TextUtils.isEmpty(account)) {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.empty_ali_account);
                            return;
                        }
                        if (TextUtils.isEmpty(mImagePath)) {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.paypal3);
                            return;
                        }
                        payName = "alipay";
                    } else if (App.INSTANCE.getString(R.string.paypal).equals(mSelectName)) {  // payPal支付
                        if (TextUtils.isEmpty(paypalAccount)) {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.empty_ali_account);
                            return;
                        }
                        payName = "paypal";
                    }
                    if (TextUtils.isEmpty(payName)) {
                        ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.empty_pay_way_name);
                        return;
                    }
                    if (TextUtils.isEmpty(payPwd)) {
                        ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(R.string.empty_pay_pwd);
                        return;
                    }
                    // 添加/编辑   支付方式   银行 银行 银行  支付宝账号/微信账号  支付宝图片/微信图片    资金密码
                    mPresenter.requestAddPayWay(mType, payName, bankName, bankBranch, bankNum, App.INSTANCE.getString(R.string.paypal).equals(mSelectName) ? paypalAccount : account, mImagePath, payPwd);
                    break;
                case R.id.dialog_cancel:
                case R.id.dialog_close:
                    if (mAlertDialog != null) {
                        mAlertDialog.dismiss();
                    }
                    break;
                case R.id.dialog_thirdparty_pic://选择二维码图片
                    PictureSelector.create(this)
                            .openGallery(PictureMimeType.ofImage())
                            .selectionMode(PictureConfig.SINGLE)
                            .forResult(PictureConfig.CHOOSE_REQUEST);
                    break;
            }
        }
    }

    public void onPayWaySuccess(HttpData<List<PayWayBean>> body) {
        mAdapter.removeHeaderView(mFailView);
        if (body.getData().size() <= 4) {
            if (mAdapter.getFooterLayoutCount() == 0) {
                mAdapter.addFooterView(mFooterView);
            }
        }
        mAdapter.setNewData(body.getData());
    }

    public void onHttpFail() {
        if (mAdapter.getHeaderLayoutCount() == 0) {
            mAdapter.addHeaderView(mFailView);
        } else {
            mStartLading.setVisibility(View.VISIBLE);
            mLoadingProgress.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 当支付方式开关切换成功
     *
     * @param item
     */
    public void onChangeSwitchSuccess(PayWayBean item) {
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 当添加或修改支付方式成功
     */
    public void onChangePayWaySuccess() {
        mImagePath = "";
        if (mAlertDialog != null) {
            mAlertDialog.dismiss();
        }
    }

    public void onRelieveBusinessSuccess() {
        mUserInfo.is_shop = false;
        mSecurityApplyBusinessChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity221));
    }
}
